package com.ruyuan.seckilling.render.controller;

import com.ruyuan.seckilling.common.MapResult;
import com.ruyuan.seckilling.render.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author zhonghuashishan
 */
@RestController
@RequestMapping("/pageRender/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @GetMapping
    public String downloadFile(String url) {
        return fileService.download(url);
    }

    @PostMapping
    public MapResult uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String content = new String(file.getBytes());
        String fileUrl = fileService.upload(content);
        return MapResult.successResult().set("fileUrl", fileUrl);
    }

}